using System;
using System.Collections.Generic;
using System.Web.UI;

namespace Merbla.WebForms.AjaxControls
{
    public  class ItemControl : Control, IScriptControl
    {
 



        public string ClientClickFunction
        {
            get
            {
                object value = ViewState["clientClickFunction"];

                return (value == null) ? string.Empty : (string)value;
            }
            set
            {
                ViewState["clientClickFunction"] = value;
            }
        }

        public string ParameterName
        {
            get
            {
                object value = ViewState["parameterName"];

                return (value == null) ? string.Empty : (string)value;
            }
            set
            {
                ViewState["parameterName"] = value;
            }
        }


        
        public string ParameterValue
        {
            get
            {
                object value = ViewState["parameterValue"];

                return (value == null) ? string.Empty : (string)value;
            }
            set
            {
                ViewState["parameterValue"] = value;
            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

            if (scriptManager == null)
            {
                throw new InvalidOperationException("ScriptManager required on the page.");
            }

            scriptManager.RegisterScriptControl(this);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
             writer.Write("Merbla.WebForms.AjaxControls.ItemControl");
             writer.RenderEndTag();

            ScriptManager.GetCurrent(this.Page).RegisterScriptDescriptors(this);
            
        }

        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            ScriptControlDescriptor desc = new ScriptControlDescriptor("Merbla.WebForms.AjaxControls.ItemControl", ClientID);

            if (!string.IsNullOrEmpty(ParameterName))
            {
                desc.AddProperty("parameterName", ParameterName);
            }


            if (!string.IsNullOrEmpty(ParameterName))
            {
                desc.AddProperty("parameterValue", ParameterValue);
            }

            if (!string.IsNullOrEmpty(ClientClickFunction))
            {
               desc.AddEvent("click", ClientClickFunction);
            }
             

            yield return desc;
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            yield return new ScriptReference(Page.ResolveUrl("~/AjaxControls/ItemControl.js"));
        }
    }
}